package cn.iocoder.mall.resource.controller;

import cn.iocoder.common.utils.common.annotation.AutoLog;
import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.common.utils.common.system.query.QueryGenerator;
import cn.iocoder.common.utils.common.util.ObjectUtils;
import cn.iocoder.mall.resource.dataobject.ShopType;
import cn.iocoder.mall.resource.service.IShopTypeService;
import cn.iocoder.mall.resource.vo.ShopTypeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 商品类目/商户类型
 * @Author: jeecg-boot
 * @Date: 2019-10-30
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "商品类目/商户类型")
@RestController
@RequestMapping("/shopType")
public class ShopTypeController {
    @Autowired
    @Qualifier("shopTypeServiceImpl")
    private IShopTypeService shopTypeService;

    /**
     * 分页列表查询
     *
     * @param ShopType
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-分页列表查询")
    @ApiOperation(value = "商品类目/商户类型-分页列表查询", notes = "商品类目/商户类型-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ShopType>> queryPageList(ShopType ShopType,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        Result<IPage<ShopType>> result = new Result<IPage<ShopType>>();
        QueryWrapper<ShopType> queryWrapper = QueryGenerator.initQueryWrapper(ShopType, req.getParameterMap());
        Page<ShopType> page = new Page<ShopType>(pageNo, pageSize);
        queryWrapper.isNull("parent_id");
        IPage<ShopType> pageList = shopTypeService.list(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param ShopType
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-添加")
    @ApiOperation(value = "商品类目/商户类型-添加", notes = "商品类目/商户类型-添加")
    @PostMapping(value = "/add")
    public Result<ShopType> add(@RequestBody ShopType ShopType) {
        Result<ShopType> result = new Result<ShopType>();
        try {
            shopTypeService.save(ShopType);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param shopType
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-编辑")
    @ApiOperation(value = "商品类目/商户类型-编辑", notes = "商品类目/商户类型-编辑")
    @PutMapping(value = "/edit")
    public Result<ShopType> edit(@RequestBody ShopType shopType) {
        Result<ShopType> result = new Result<ShopType>();
        ShopType shopTypeEntity = shopTypeService.getById(shopType.getId());
        if (shopTypeEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = shopTypeService.updateById(shopType);
            //TODO 返回false说明什么？
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-通过id删除")
    @ApiOperation(value = "商品类目/商户类型-通过id删除", notes = "商品类目/商户类型-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            shopTypeService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-批量删除")
    @ApiOperation(value = "商品类目/商户类型-批量删除", notes = "商品类目/商户类型-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<ShopType> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        Result<ShopType> result = new Result<ShopType>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.shopTypeService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "商品类目/商户类型-通过id查询")
    @ApiOperation(value = "商品类目/商户类型-通过id查询", notes = "商品类目/商户类型-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ShopType> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<ShopType> result = new Result<ShopType>();
        ShopType ShopType = shopTypeService.getById(id);
        if (ShopType == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(ShopType);
            result.setSuccess(true);
        }
        return result;
    }


    @GetMapping("/getShopTypeList")
    public Result<?> getShopTypeList() {

        return Result.ok(shopTypeService.getShopTypeList(1));

    }

    @GetMapping("/getGoodsTypeList")
    public Result<?> getGoodsTypeList() {

        return Result.ok(shopTypeService.getShopTypeList(2));

    }

    @GetMapping("/getShopTypeListOfNoTree")
    public Result<?> getShopTypeListOfNoTree() {

        List<ShopType> list = shopTypeService.getShopTypeListOfNoTree(1);
        return Result.ok(ObjectUtils.copyList(list, ShopTypeVO.class));

    }

    @GetMapping("/getGoodsTypeListOfFirstLevel")
    public Result<?> getShopTypeListByType() {

        return Result.ok(shopTypeService.getShopTypeListByTypeAndLevel(1, 1));

    }

    @GetMapping("/getShopTypeListByParentId")
    public Result<?> getShopTypeListByParentId(@RequestParam Integer id) {

        return Result.ok(shopTypeService.getShopTypeListByParentId(id));

    }

    @GetMapping("/getShopTypeListByName")
    public Result<?> getShopTypeListByName(@RequestParam("name") String name) {

        return Result.ok(shopTypeService.getShopTypeListByName(name));

    }
}
